<template>
  <template v-for="item in uploadList">
    <div class="demo-upload-list" v-if="showImg">
      <Image :src="item.url" fit="cover" width="100%" height="100%" />
      <div class="demo-upload-list-cover">
        <Icon type="ios-eye-outline" @click="handleView(item.name)"></Icon>
        <Icon type="ios-trash-outline" @click="handleRemove(item)"></Icon>
      </div>
    </div>
  </template>
  <Upload
    v-if="!readonly"
    ref="upload"
    :show-upload-list="false"
    :headers="headers"
    :on-success="handleSuccess"
    name="file"
    :max-size="2048"
    :on-format-error="handleFormatError"
    :on-exceeded-size="handleMaxSize"
    :before-upload="handleBeforeUpload"
    :on-preview="onPreview"
    :default-file-list="defaultList"
    multiple
    type="drag"
    action="/api/oss/upload"
    v-bind="uploadProps"
    :style="showImg ? style : ''"
  >
    <slot>
      <div style="width: 58px; height: 58px; line-height: 58px">
        <Icon type="ios-camera" size="20"></Icon>
      </div>
    </slot>
  </Upload>
  <ImagePreview v-model="visible" :preview-list="[]" />
</template>
<script>
import cookies from "@/libs/util.cookies";
import { nextTick } from "vue";
export default {
  props: {
    modelValue: {
      type: String,
      default: "",
    },
    uploadProps: {
      type: Object,
      default: () => {},
    },
    showImg: {
      type: Boolean,
      default: true,
    },
    style: {
      type: String,
      default: 'display: inline-block;width:58px;"',
    },
    readonly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      imgName: "",
      visible: false,
      uploadList: [],
      headers: {
        "X-Application-name": "supe",
        Authorization: `bearer ${cookies.get("token")}`,
      },
      defaultList: [],
    };
  },
  watch: {
    modelValue: {
      immediate: true,
      deep: true,
      handler(nVal) {
        this.defaultList = [];
        if (!Array.isArray(nVal) && nVal) {
          this.uploadList = [];
          let imgList = nVal.split(",");
          let imgAry = [];
          imgList.map((item, index) => {
            const arr = item.split("/");
            const name = arr[arr.length - 1];
            imgAry.push({
              url: item,
              name: name,
              percentage: 100,
              status: "finished",
              uid: Date.now(),
            });
          });
          this.uploadList = imgAry;
          this.defaultList = imgAry;
        } else {
          this.uploadList = [];
        }
      },
    },
  },
  methods: {
    handleView(name) {
      this.imgName = name;
      this.visible = true;
    },
    handleRemove(file) {
      const fileList = this.$refs.upload.fileList;
      this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
      const value = fileList.map((item) => item.url).join(",");
      this.$parent.$emit("on-form-change");
      this.$emit("update:modelValue", value);
      this.$emit("change");
    },
    handleSuccess(res, file, fileList) {
      if (file.response.code == 200) {
        file.url = `${file.response.data.fileUrl}${file.response.data.fileFormat}`;
        file.name = file.response.data.fileName;
        const value = fileList.map((item) => item.url).join(",");
        this.$emit("update:modelValue", value);
        this.$emit("change");
      } else {
        console.log("上传失败");
      }
    },
    onPreview(file) {
      location.href = file.url;
    },
    handleFormatError(file) {
      this.$Notice.warning({
        title: "提示",
        desc: "你上传的文件格式不正确",
      });
    },
    handleMaxSize(file) {
      this.$Notice.warning({
        title: "提示",
        desc: "你上传的文件大小超过限制",
      });
    },
    handleBeforeUpload() {
      const check = this.uploadList.length < 5;
      if (!check) {
        this.$Notice.warning({
          title: "你上传的文件个数超过限制",
        });
      }
      return check;
    },
  },
};
</script>
<style lang="less" scoped>
.demo-upload-list {
  display: inline-block;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}

.demo-upload-list img {
  width: 100%;
  height: 100%;
}

.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}

.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}

.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>
